package org.example;

public class Disorder {
    static int x,y,a,b;
    public static void main(String[] args) throws InterruptedException {
        int j = 0;
        for (;;) {
            j++;
            x = 0; y = 0;
            a = 0; b = 0;
            Thread one = new Thread(new Runnable() {
                @Override
                public void run() {
                    a = 1;
                    x = b;
                }
            });

            Thread other = new Thread(new Runnable() {
                @Override
                public void run() {
                    b = 1;
                    y = a;
                }
            });
            one.start();
            other.start();
            one.join(); // 当前（主）线程阻塞，让 one 先执行
            other.join();
            if (x == 0 && y == 0) {
                System.err.println("第 " + j + " 次, x = 0 && y = 0");
                break;
            }
        }
    }
}
